package geo.com.geosdkdemo.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import geo.com.geosdk.GeoManage;
import geo.com.geosdk.listener.DeviceCommandSendListener;
import geo.com.geosdk.modelsetting.GeneralBean.SateSystemEnableParameter;
import geo.com.geosdk.modelsetting.StatisModel.StaticWorkingModeParameter;
import geo.com.geosdkdemo.R;
import geo.com.geosdkdemo.event.DialogDismissEvent;

import static android.view.View.VISIBLE;


public class StaticSetupActivity extends AppCompatActivity implements View.OnClickListener{
    public static final String DIALOGNAME = "DIALOGNAME";
    private StaticWorkingModeParameter staticWorkingModeParameter= GeoManage.getInstance().getStaticWorkingModeParameter();
    private SateSystemEnableParameter systemEnableParameter= GeoManage.getInstance().getSystemEnableParameter();
    private ImageView ivCommonBack;
    private TextView tvCommonTitle;
    private EditText etElevationMask;
    private EditText etPdop;
    private EditText etRecordPointname;
    private EditText etRecordInterval;
    private ImageView ivSwitchRecordStaticOn;
    private ImageView ivSwitchRecordStaticOff;
    private EditText etMeasureHeight;
    private RelativeLayout rlMeasureMode;
    private TextView tvMeasureMode;
    private Button btnDetermine;
    private FrameLayout flLoading;
    private ImageView ivLoading;
    private TextView tvLoadingTitle;
    private Animation anim;
    private ImageView ivSwitchGpsOn;
    private ImageView ivSwitchGpsOff;
    private ImageView ivSwitchGlonassOn;
    private ImageView ivSwitchGlonassOff;
    private ImageView ivSwitchBeidouOn;
    private ImageView ivSwitchBeidouOff;
    private ImageView ivSwitchGalileoOn;
    private ImageView ivSwitchGalileoOff;
    private ImageView ivSwitchSbasOn;
    private ImageView ivSwitchSbasOff;
    private ImageView ivSwitchQzssOn;
    private ImageView ivSwitchQzssOff;
    private String str;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_static_setup);
        initView();
        initData();
    }

    private void initView() {
        ivCommonBack=findViewById(R.id.iv_common_back);
        tvCommonTitle=findViewById(R.id.tv_common_title);
        etElevationMask = (EditText)findViewById( R.id.et_elevation_mask );
        etPdop = (EditText)findViewById( R.id.et_pdop );
        etRecordPointname = (EditText)findViewById( R.id.et_record_pointname );
        etRecordInterval = (EditText)findViewById( R.id.et_record_interval );
        ivSwitchRecordStaticOn = (ImageView)findViewById( R.id.iv_switch_record_static_on );
        ivSwitchRecordStaticOff = (ImageView)findViewById( R.id.iv_switch_record_static_off );
        etMeasureHeight = (EditText)findViewById( R.id.et_measure_height );
        rlMeasureMode = (RelativeLayout)findViewById( R.id.rl_measure_mode );
        tvMeasureMode = (TextView)findViewById( R.id.tv_measure_mode );
        btnDetermine = (Button)findViewById( R.id.btn_determine );
        flLoading = (FrameLayout)findViewById( R.id.fl_loading );
        ivLoading = (ImageView)findViewById( R.id.iv_loading );
        tvLoadingTitle = (TextView)findViewById( R.id.tv_loading_title );
        ivSwitchGpsOn = (ImageView)findViewById( R.id.iv_switch_gps_on );
        ivSwitchGpsOff = (ImageView)findViewById( R.id.iv_switch_gps_off );
        ivSwitchGlonassOn = (ImageView)findViewById( R.id.iv_switch_glonass_on );
        ivSwitchGlonassOff = (ImageView)findViewById( R.id.iv_switch_glonass_off );
        ivSwitchBeidouOn = (ImageView)findViewById( R.id.iv_switch_beidou_on );
        ivSwitchBeidouOff = (ImageView)findViewById( R.id.iv_switch_beidou_off );
        ivSwitchGalileoOn = (ImageView)findViewById( R.id.iv_switch_galileo_on );
        ivSwitchGalileoOff = (ImageView)findViewById( R.id.iv_switch_galileo_off );
        ivSwitchSbasOn = (ImageView)findViewById( R.id.iv_switch_sbas_on );
        ivSwitchSbasOff = (ImageView)findViewById( R.id.iv_switch_sbas_off );
        ivSwitchQzssOn = (ImageView)findViewById( R.id.iv_switch_qzss_on );
        ivSwitchQzssOff = (ImageView)findViewById( R.id.iv_switch_qzss_off );

        ivCommonBack.setOnClickListener(this);
        ivSwitchGpsOn.setOnClickListener(this);
        ivSwitchGpsOff.setOnClickListener(this);
        ivSwitchGlonassOn.setOnClickListener(this);
        ivSwitchGlonassOff.setOnClickListener(this);
        ivSwitchBeidouOn.setOnClickListener(this);
        ivSwitchBeidouOff.setOnClickListener(this);
        ivSwitchGalileoOff.setOnClickListener(this);
        ivSwitchGalileoOn.setOnClickListener(this);
        ivSwitchSbasOff.setOnClickListener(this);
        ivSwitchSbasOn.setOnClickListener(this);
        ivSwitchQzssOn.setOnClickListener(this);
        ivSwitchQzssOff.setOnClickListener(this);
        rlMeasureMode.setOnClickListener(this);
        btnDetermine.setOnClickListener(this);
        ivSwitchRecordStaticOn.setOnClickListener(this);
        ivSwitchRecordStaticOff.setOnClickListener(this);

    }

    private void initData() {
        tvCommonTitle.setText("静态站设置");
        if (!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);
        }

        //进来显示接收机解析的数据
        showRTKData();
    }

    private void showRTKData() {
        //显示通用
        etElevationMask.setText(String.valueOf(staticWorkingModeParameter.mEleMask));
        etPdop.setText(String.valueOf(staticWorkingModeParameter.mPdopThreshold));

        etRecordPointname.setText(staticWorkingModeParameter.mStatisPointName);
        etRecordInterval.setText(String.valueOf(staticWorkingModeParameter.mSampleRate));

        if(staticWorkingModeParameter.mAutoRecord){
            onClickRecordStaticOff();
        }else{
            onClickRecordStaticOn();
        }

        etMeasureHeight.setText(String.valueOf(staticWorkingModeParameter.mAntenna.dMeasureHeight));
        tvMeasureMode.setText(staticWorkingModeParameter.mAntenna.mAntennaMeasureMode.getTypeTitle(staticWorkingModeParameter.mAntenna.mAntennaMeasureMode));

        //显示卫星系统
        showSatelliteSystem();

    }

    private void showSatelliteSystem() {
        if(systemEnableParameter.UseGPS){
            onClickGpsOff();
        }else{
            onClickGpsOn();
        }

        if(systemEnableParameter.UseGlonass){
            onClickGlonassOff();
        }else{
            onClickGlonassOn();
        }

        if(systemEnableParameter.UseBeiDou){
            onClickBeidouOff();
        }else{
            onClickBeidouOn();
        }

        if(systemEnableParameter.UseGalileo){
            onClickGalileoOff();
        }else{
            onClickGalileoOn();
        }

        if(systemEnableParameter.UseSBAS){
            onClickSbasOff();
        }else{
            onClickSbasOn();
        }

        if(systemEnableParameter.UseQZSS){
            onClickQzssOff();
        }else{
            onClickQzssOn();
        }

        if(systemEnableParameter.UseGPS){
            onClickGpsOff();
        }else{
            onClickGpsOn();
        }


    }

    private void onClickQzssOn() {
        ivSwitchQzssOn.setVisibility(View.GONE);
        ivSwitchQzssOff.setVisibility(View.VISIBLE);
    }

    private void onClickQzssOff() {
        ivSwitchQzssOff.setVisibility(View.GONE);
        ivSwitchQzssOn.setVisibility(View.VISIBLE);
    }

    private void onClickSbasOn() {
        ivSwitchSbasOn.setVisibility(View.GONE);
        ivSwitchSbasOff.setVisibility(View.VISIBLE);
    }

    private void onClickSbasOff() {
        ivSwitchSbasOff.setVisibility(View.GONE);
        ivSwitchSbasOn.setVisibility(View.VISIBLE);
    }

    private void onClickGalileoOn() {
        ivSwitchGalileoOn.setVisibility(View.GONE);
        ivSwitchGalileoOff.setVisibility(View.VISIBLE);
    }

    private void onClickGalileoOff() {
        ivSwitchGalileoOff.setVisibility(View.GONE);
        ivSwitchGalileoOn.setVisibility(View.VISIBLE);
    }

    private void onClickBeidouOn() {
        ivSwitchBeidouOn.setVisibility(View.GONE);
        ivSwitchBeidouOff.setVisibility(View.VISIBLE);
    }

    private void onClickBeidouOff() {
        ivSwitchBeidouOff.setVisibility(View.GONE);
        ivSwitchBeidouOn.setVisibility(View.VISIBLE);
    }

    private void onClickGlonassOn() {
        ivSwitchGlonassOn.setVisibility(View.GONE);
        ivSwitchGlonassOff.setVisibility(View.VISIBLE);
    }

    private void onClickGlonassOff() {
        ivSwitchGlonassOff.setVisibility(View.GONE);
        ivSwitchGlonassOn.setVisibility(View.VISIBLE);
    }

    private void onClickGpsOn() {
        ivSwitchGpsOn.setVisibility(View.GONE);
        ivSwitchGpsOff.setVisibility(View.VISIBLE);
    }

    private void onClickGpsOff() {
        ivSwitchGpsOff.setVisibility(View.GONE);
        ivSwitchGpsOn.setVisibility(View.VISIBLE);
    }



    private void onClickMeasureMode() {
        Intent intent=new Intent(this,SelectDialogActivity.class);
        intent.putExtra(DIALOGNAME,"天线量取方式");
        startActivity(intent);
    }

    //选择对话框里面的选项后做的操作
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void showChannelNum(DialogDismissEvent event) {
        String title=event.getTitle();
        String data=event.getData();
        switch (title) {
            case "天线量取方式":
                tvMeasureMode.setText(data);
                break;
        }
    }


    @Override
    public void onClick(View v) {
        if(v==ivCommonBack){
            finish();
        }else if(v==rlMeasureMode){
            onClickMeasureMode();
        }else if(v==btnDetermine){
            onClickBtnDetermine();
        }else if(v==ivSwitchRecordStaticOff){
            onClickRecordStaticOff();
        }else if(v==ivSwitchRecordStaticOn){
            onClickRecordStaticOn();
        }else if(v==ivSwitchGpsOff){
            onClickGpsOff();
        }else if(v==ivSwitchGpsOn){
            onClickGpsOn();
        }else if(v==ivSwitchGlonassOff){
            onClickGlonassOff();
        }else if(v==ivSwitchGlonassOn){
            onClickGlonassOn();
        }else if(v==ivSwitchBeidouOff){
            onClickBeidouOff();
        }else if(v==ivSwitchBeidouOn){
            onClickBeidouOn();
        }else if(v==ivSwitchGalileoOff){
            onClickGalileoOff();
        }else if(v==ivSwitchGalileoOn){
            onClickGalileoOn();
        }else if(v==ivSwitchSbasOff){
            onClickSbasOff();
        }else if(v==ivSwitchSbasOn){
            onClickSbasOn();
        }else if(v==ivSwitchQzssOff){
            onClickQzssOff();
        }else if(v==ivSwitchQzssOn){
            onClickQzssOn();
        }
    }

    private void onClickRecordStaticOn() {
        ivSwitchRecordStaticOn.setVisibility(View.GONE);
        ivSwitchRecordStaticOff.setVisibility(View.VISIBLE);
    }

    private void onClickRecordStaticOff() {
        ivSwitchRecordStaticOff.setVisibility(View.GONE);
        ivSwitchRecordStaticOn.setVisibility(View.VISIBLE);
    }

    private void onClickBtnDetermine() {
        //开始设置命令
        //1.开始动画
        startAnim();
        //2.监听发送状态
        GeoManage.getInstance().setDeviceCommandSendListener(new DeviceCommandSendListener() {
            @Override
            public void OnDeviceCommandSendCallBack(final int status, final String strFunction) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(status==0){
                            str="开始"+strFunction;
                        }else if(status==1){
                            str=strFunction+"完成";
                        }else if(status==2){
                            str=strFunction+"超时";
                            //超时这边的操作是继续发送下一条命令
                        }else if(status==3){//全部发送成功
                            str="全部命令发送完成";
                            stopAnim();
                            finish();
                        }
                        tvLoadingTitle.setText(str);
                        Log.d("OnDeviceCommandSend", "OnDeviceCommandSendCallBack: "+str);
                    }
                });

            }
        });

        //设置参数
        //1.设置静态站通用选项
        StaticWorkingModeParameter staticWorkingModeParameter=new StaticWorkingModeParameter();
        staticWorkingModeParameter.mEleMask= Integer.parseInt(etElevationMask.getText().toString().trim());
        staticWorkingModeParameter.mPdopThreshold= Double.parseDouble(etPdop.getText().toString().trim());
        staticWorkingModeParameter.mStatisPointName=etRecordPointname.getText().toString().trim();
        staticWorkingModeParameter.mSampleRate= Integer.parseInt(etRecordInterval.getText().toString().trim());
        staticWorkingModeParameter.mAutoRecord = ivSwitchRecordStaticOn.getVisibility() == VISIBLE;

        staticWorkingModeParameter.mAntenna.dMeasureHeight= Double.parseDouble(etMeasureHeight.getText().toString().trim());
        staticWorkingModeParameter.mAntenna.mAntennaMeasureMode=staticWorkingModeParameter.mAntenna.mAntennaMeasureMode.titleToEnum(tvMeasureMode.getText().toString().trim());


        //2.初始化SateSystemEnableParameter，设置是否使用6大卫星参数
        SateSystemEnableParameter sateSystemEnableParameter=new SateSystemEnableParameter();
        sateSystemEnableParameter.UseGPS = ivSwitchGpsOn.getVisibility() == View.VISIBLE;
        sateSystemEnableParameter.UseGlonass = ivSwitchGlonassOn.getVisibility() == View.VISIBLE;
        sateSystemEnableParameter.UseBeiDou = ivSwitchBeidouOn.getVisibility() == View.VISIBLE;
        sateSystemEnableParameter.UseGalileo=ivSwitchGalileoOn.getVisibility() == View.VISIBLE;
        sateSystemEnableParameter.UseQZSS=ivSwitchQzssOn.getVisibility() == View.VISIBLE;
        sateSystemEnableParameter.UseSBAS=ivSwitchSbasOn.getVisibility() == View.VISIBLE;

        //3.调用接口进行命令发送
        GeoManage.getInstance().setStaticModel(staticWorkingModeParameter,sateSystemEnableParameter);

    }

    //开始动画
    private void startAnim() {
        anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim_round_rotate);
        LinearInterpolator interpolator = new LinearInterpolator();  //设置匀速旋转，在xml文件中设置会出现卡顿
        anim.setInterpolator(interpolator);
        if (anim != null) {
            flLoading.setVisibility(VISIBLE);
            ivLoading.startAnimation(anim);  //开始动画
        }
    }

    //停止动画
    private void stopAnim() {
        if (anim != null) {
            anim =null;
            flLoading.setVisibility(View.GONE);
            ivLoading.clearAnimation();
        }
    }

    public void showToast(String str) {
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        if (EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this);
        }
        stopAnim();
        super.onDestroy();
    }
}
